package com.ruoyi.ftrl.mqtt.service.impl;

import com.ruoyi.ftrl.mqtt.service.MqttService;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MqttServiceImpl implements MqttCallback, MqttService {

    @Autowired
    private MqttClient mqttClient;

    public void publishMessage(String topic, String payload) {
        try {
            mqttClient.publish(topic, new MqttMessage(payload.getBytes()));
            System.out.println("Published to " + topic + ": " + payload);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void connectionLost(Throwable cause) {
        System.out.println("MQTT connection lost: " + cause);
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) {
        System.out.println("Message received from " + topic + ": " + message.toString());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("Delivery complete for token: " + token.getMessageId());
    }

    @Override
    public boolean connect() {
        return false;
    }

    @Override
    public boolean subscribe(String topic, int qos) {
        return false;
    }

    @Override
    public boolean publish(String topic, String payload, int qos) {
        return false;
    }

    @Override
    public void disconnect() {

    }

    @Override
    public boolean isConnected() {
        return false;
    }
}